package com.qf.sy2103.ssm.Service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.sy2103.ssm.Dao.UserDao;
import com.qf.sy2103.ssm.Service.UserService;
import com.qf.sy2103.ssm.entity.User;
import com.qf.sy2103.ssm.vo.Tablevo;
import com.qf.sy2103.ssm.vo.pagevo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private  UserDao userDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addUser(User user) {
//        int j=1/0;
        int i = userDao.addUser(user);
        return i;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User selectUser(String name) {
        User user = userDao.selectUser(name);
        return user;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User findUser(String name, String password) {
        User user = userDao.findUser(name, password);
        return user;
    }

    //实现分页查询引入 pageHelper
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Tablevo<User> getALLUser(pagevo pagevo) {
        //指定一下sql语句的分页信息
      PageHelper.startPage(pagevo.getPage(),pagevo.getLimit());
      List<User> list =  userDao.getAllUser();
      PageInfo<User> userPageInfo = new PageInfo<>(list);

      return new Tablevo<User>(0,"ok",userPageInfo.getTotal(),list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteUser(List<Integer> list) {
//        int j = 1/0;
        int i = userDao.deleteList(list);
        return i;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateUser(User user) {
        int i = userDao.updateUser(user);
        return i;
    }
}
